package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisMessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.mapper.CheckItemMapper;
import com.itheima.mapper.SetmealMapper;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Setmeal;
import com.itheima.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.JedisPool;

import java.util.List;

@Service(interfaceClass = CheckItemService.class)
public class CheckItemServiceImpl implements CheckItemService {

    @Autowired
    private CheckItemMapper checkItemMapper;

    @Autowired
    public SetmealMapper setmealMapper;

    @Autowired
    private JedisPool jedisPool;


    @Override
    public void add(CheckItem checkItem) {
        checkItemMapper.add(checkItem);
    }

   /* @Override
    public PageResult findPage(Integer currentPage, Integer pageSize, String queryString) {

        //定义limit x,y中的x的值
        int firstResult = (currentPage-1)*pageSize;

        //查询总记录数
        Long count = checkItemMapper.findCountByCondition(queryString);

        //查询当前页的集合
        List<CheckItem> list = checkItemMapper.findCheckItemListByCondition(firstResult,pageSize,queryString);

        return new PageResult(count,list);
    }*/

    @Override
    public PageResult findPage(Integer currentPage, Integer pageSize, String queryString) {

        //定义当前页的页码和页面大小
        PageHelper.startPage(currentPage,pageSize);

        //条件查询数据
        List<CheckItem> list = checkItemMapper.findByCondition(queryString);

        //将查询的数据给PageInfo进行包装
        PageInfo<CheckItem> pageInfo = new PageInfo<>(list);

        return new PageResult(pageInfo.getTotal(),pageInfo.getList());
    }

    @Override
    public void deleteById(Integer id) {

        //先通过id查询当前检查项有没有被检查组引用
        Integer count =  checkItemMapper.findCountByCheckItemId(id);
        if (count>0){
            //抛出异常，提示不能删除
            throw new RuntimeException(MessageConstant.CHECKITEM_IS_QUOTED);
        }
        //删除检查项
        checkItemMapper.deleteById(id);

    }

    @Override
    public CheckItem findById(Integer id) {
        return checkItemMapper.findById(id);
    }

    @Override
    public void edit(CheckItem checkItem) {
        checkItemMapper.edit(checkItem);
        //查询和此检查项有关的套餐，并将缓存内的套餐细节删除
        List<Setmeal> list =setmealMapper.findByCheckItem(checkItem.getId());
        for (Setmeal setmeal : list) {
            jedisPool.getResource().del(setmeal.getId()+ RedisMessageConstant.SETMEAL_DETAIL);
        }
    }

    /**
     * 查询所有检查项
     * @return 返回一个List<CheckItem>
     */
    @Override
    public List<CheckItem> findAll() {
        return checkItemMapper.findAll();
    }

    @Override
    public List<Integer> findCheckItemIdsByCheckGroupId(Integer id) {
        return checkItemMapper.findCheckItemIdsByCheckGroupId(id);
    }
}
